/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListadoUnidadesConstructivas.java
 *
 * Created on 09-may-2011, 18:41:13
 */
package es.fpsampayo.fin2006.gui.paneles;

import es.fpsampayo.fin2006.gui.forms.CatFormUnidadesConstructivas;
import es.fpsampayo.recursos.Conexion;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fran
 */
public class ListadoUnidadesConstructivas extends javax.swing.JPanel {
    
    int id_parcela;

    /** Creates new form ListadoUnidadesConstructivas */
    public ListadoUnidadesConstructivas() {
        initComponents();
        
    }
    
    public void cargarUnidades (int id_parcela){
        DefaultTableModel modelo = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        try {
            String sql = "SELECT * FROM cat.unidad_construcctiva WHERE id_parcela = "+ id_parcela + ";";
            System.out.println(sql);
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int ncol = metadata.getColumnCount();
            String campos[] = new String [ncol];
            for ( int i = 0; i < ncol ; i++)
                campos[i] = metadata.getColumnLabel(i+1);
            modelo.setColumnIdentifiers(campos);
            while (rs.next()){
                Object[] fila = new Object[ncol];
                for ( int i = 0; i < ncol; i++){
                    try{
                        fila[i] = rs.getObject(i + 1);
                    } catch (SQLException ex) {
                        Logger.getLogger(ListadoUnidadesConstructivas.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                modelo.addRow(fila);
            }

            conexion.Desconectar();

        } catch (SQLException ex) {
            Logger.getLogger(ListadoUnidadesConstructivas.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTable1.setModel(modelo);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        ocultarColumnas();
    }
    
    public void ocultarColumnas(){
        
        int columnas[] = new int[]{0,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,25,26,27,28};
        
        for(int i = 0; i < columnas.length; i++){
            jTable1.getColumnModel().getColumn(columnas[i]).setMaxWidth(0);
            jTable1.getColumnModel().getColumn(columnas[i]).setMinWidth(0);
            jTable1.getColumnModel().getColumn(columnas[i]).setPreferredWidth(0);
        }
        jTable1.getColumnModel().getColumn(6).setHeaderValue("U. Constr.");
        jTable1.getColumnModel().getColumn(22).setHeaderValue("Año");
        jTable1.getColumnModel().getColumn(24).setHeaderValue("Sup. (m2)");
                
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnVer = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(660, 430));
        setMinimumSize(new java.awt.Dimension(660, 430));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(jTable1);

        btnVer.setText("Ver");
        btnVer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
                    .addComponent(btnVer))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnVer)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnVerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerActionPerformed
        
        id_parcela = Integer.parseInt(jTable1.getValueAt(jTable1.getSelectedRow(), 1).toString());
        String sql = "SELECT * FROM cat.unidad_construcctiva WHERE id_parcela = "+ id_parcela + ";";
        System.out.println(sql);
        Conexion conexion = new Conexion();
        ResultSet rs = conexion.Select(sql);
        int id_uc = Integer.parseInt(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString());
        
        try {
            CatFormUnidadesConstructivas unidades = new CatFormUnidadesConstructivas(null, false, rs, id_uc);
            unidades.cargarCampos();
            unidades.setLocationRelativeTo(this);
            unidades.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(ListadoUnidadesConstructivas.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_btnVerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnVer;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
